﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class NodeEditor : Form
    {
        Node node;
        public NodeEditor(Node n)
        {
            node = n;
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            edit_nodename.Text = node.name;
            edit_hotvalue.Text = node.hotvalue.ToString();
            edit_starttime.Text = node.start_time.ToString();
            listBox_plots.BeginUpdate();
            listBox_plots.Items.Clear();
            foreach(Plot p in node.plots)
            {
                listBox_plots.Items.Add(p.name);
            }
            listBox_plots.EndUpdate();

            

        }

        private void NodeEditor_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            node.plots.Add(new Plot() { name = "新剧情" });
            UpdateDisplay();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            int idx = listBox_plots.SelectedIndex;
            if (idx < 0) return;
            if (MessageBox.Show(string.Format("确定要删除剧情 {0} 吗？", node.plots[idx].name), "确定删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                node.plots.RemoveAt(idx);
                UpdateDisplay();
            }
        }

        private void listBox_plots_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = listBox_plots.SelectedIndex;
            if (idx < 0) return;
            PlotEditor pe = new PlotEditor(node.plots[idx]);
            pe.Show();
        }

        private void edit_nodename_TextChanged(object sender, EventArgs e)
        {
            node.name = edit_nodename.Text;
        }

        private void NodeEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.mainform.UpdateDisplay();
        }

        private void edit_starttime_TextChanged(object sender, EventArgs e)
        {
            try
            {
                node.start_time = int.Parse(edit_starttime.Text);
            }
            catch (FormatException)
            {
                //MessageBox.Show("请输入一个整数", "输入异常");
            }
            
        }

        private void edit_hotvalue_TextChanged(object sender, EventArgs e)
        {
            try
            {
                node.hotvalue = int.Parse(edit_hotvalue.Text);
            }
            catch (FormatException)
            {
                //MessageBox.Show("请输入一个整数", "输入异常");
            }
        }
    }
}
